﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StudentInfo.Areas.StudentArea.ViewModelHelpers;
using StudentInfo.Areas.StudentArea.Services;
using StudentInfo.Areas.StudentArea.ViewModels;
using StudentInfo.Security.Enums;
using StudentInfo.Security.Attributes;

namespace StudentInfo.Areas.StudentArea.Controllers
{
    [Permissions(PermissionType.GeneralResources)]
    public class MarkController : Controller
    {
        //
        // GET: /StudentArea/Mark/

        public ActionResult Index()
        {
            var marks = MarkDetailsViewModelHelper.PopulateMarkDetailsViewModelList(MarkService.GetAll());
            return View(marks);
        }

        public ActionResult Details(int markId)
        {
            var mark = MarkService.Get(markId);

            if (mark != null)
                return View(MarkDetailsViewModelHelper.PopulateMarkDetailsViewModel(mark));

            return RedirectToRoute("Mark", new { action = "Index" });
        }

        [Permissions(PermissionType.GeneralResources, CrudType.CanUpdate)]
        public ActionResult Edit(int markId)
        {
            var mark = MarkService.Get(markId);
            if (mark == null)
                RedirectToRoute("Mark", new { action = "Index" });

            var model = MarkEditViewModelHelper.PopulateMarkEditViewModel(mark);
            var students = StudentService.GetAll();
            MarkEditViewModelHelper.PopulateStudentList(model, students);
            var exams = ExamService.GetAll();
            MarkEditViewModelHelper.PopulateExamList(model, exams);

            return View(model);
        }

        [HttpPost]
        [Permissions(PermissionType.GeneralResources, CrudType.CanUpdate)]
        public ActionResult Edit(MarkEditViewModel model)
        {
            var mark = MarkEditViewModelHelper.PopulateMarkFromEditViewModel(model);

            if (mark != null)
                MarkService.Save(mark);

            return RedirectToRoute("Mark", new { action = "Details", markId = model.Id });
        }

        [Permissions(PermissionType.GeneralResources, CrudType.CanCreate)]
        public ActionResult Create()
        {
            var model = new MarkEditViewModel();
            var students = StudentService.GetAll();
            MarkEditViewModelHelper.PopulateStudentList(model, students);
            var exams = ExamService.GetAll();
            MarkEditViewModelHelper.PopulateExamList(model, exams);

            return View(model);
        }

        [HttpPost]
        [Permissions(PermissionType.GeneralResources, CrudType.CanCreate)]
        public ActionResult Create(MarkEditViewModel model)
        {
            var mark = MarkEditViewModelHelper.PopulateMarkFromEditViewModel(model);

            if (mark != null)
                MarkService.Create(mark);

            return RedirectToRoute("Mark", new { action = "Index" });
        }

        [Permissions(PermissionType.GeneralResources, CrudType.CanDelete)]
        public ActionResult Delete(int markId)
        {
            var mark = MarkService.Get(markId);

            if (mark != null)
                return View(MarkDetailsViewModelHelper.PopulateMarkDetailsViewModel(mark));

            return RedirectToRoute("Mark", new { action = "Index" });
        }

        [HttpPost]
        [Permissions(PermissionType.GeneralResources, CrudType.CanDelete)]
        public ActionResult Delete(int markId, FormCollection collection)
        {
            var mark = MarkService.Get(markId);

            if (mark != null)
                MarkService.Delete(mark);

            return RedirectToRoute("Mark", new { action = "Index" });
        }

    }
}
